<?php

namespace wework;

use wework\struct\serviceCorp\SetSessionInfoReq;
use wework\struct\serviceCorp\GetPermanentCodeRsp;
use wework\struct\serviceCorp\GetAuthInfoRsp;
use wework\struct\serviceCorp\GetAdminListRsp;
use wework\struct\serviceCorp\GetUserinfoBy3rdRsp;
use wework\struct\serviceCorp\GetUserDetailBy3rdRsp;

/*
 * Copyright (C) 2017 All rights reserved.
 *
 * @File ServiceCorpAPI.class.php
 * @Brief : 为服务商开放的接口, 使用应用授权的token
 * @Author abelzhu, abelzhu@tencent.com
 * @Version 1.0
 * @Date 2017-12-26
 *
 */
class ServiceCorpAPI extends CorpAPI
{
    /** @var string */
    private $suite_id = null;
    /** @var string */
    private $suite_secret = null;
    /** @var string */
    private $suite_ticket = null;

    /** @var string */
    private $authCorpId = null;
    /** @var string */
    private $permanentCode = null;

    /** @var string */
    private $suiteAccessToken = null;

    public function __construct(
        $suite_id = null,
        $suite_secret = null,
        $suite_ticket = null,
        $authCorpId = null,
        $permanentCode = null
    ) {
        $this->suite_id = $suite_id;
        $this->suite_secret = $suite_secret;
        $this->suite_ticket = $suite_ticket;

        // 调用 CorpAPI 的function， 需要设置这两个参数
        $this->authCorpId = $authCorpId;
        $this->permanentCode = $permanentCode;
    }

    /**
     * @brief RefreshAccessToken : override CorpAPI的函数，使用三方服务商的get_corp_token
     *
     * @return string
     */
    protected function RefreshAccessToken()
    {
        Utils::checkNotEmptyStr($this->authCorpId, "auth_corpid");
        Utils::checkNotEmptyStr($this->permanentCode, "permanent_code");
        $args = array(
            "auth_corpid" => $this->authCorpId,
            "permanent_code" => $this->permanentCode
        );
        $url = HttpUtils::MakeUrl("/cgi-bin/service/get_corp_token?suite_access_token=SUITE_ACCESS_TOKEN");
        $this->_HttpPostParseToJson($url, $args, false);
        $this->_CheckErrCode();

        $this->accessToken = $this->rspJson["access_token"];
    }

    /**
     * @brief GetSuiteAccessToken : 获取第三方应用凭证
     *
     * @link https://work.weixin.qq.com/api/doc#10975/获取第三方应用凭证
     *
     * @note 调用者不用关心，本类会自动获取、更新
     *
     * @return string
     */
    protected function GetSuiteAccessToken()
    {
        if (!Utils::notEmptyStr($this->suiteAccessToken)) {
            $this->RefreshSuiteAccessToken();
        }
        return $this->suiteAccessToken;
    }

    protected function RefreshSuiteAccessToken()
    {
        Utils::checkNotEmptyStr($this->suite_id, "suite_id");
        Utils::checkNotEmptyStr($this->suite_secret, "suite_secret");
        Utils::checkNotEmptyStr($this->suite_ticket, "suite_ticket");
        $args = array(
            "suite_id" => $this->suite_id,
            "suite_secret" => $this->suite_secret,
            "suite_ticket" => $this->suite_ticket,
        );
        $url = HttpUtils::MakeUrl("/cgi-bin/service/get_suite_token");
        $this->_HttpPostParseToJson($url, $args, false);
        $this->_CheckErrCode();

        $this->suiteAccessToken = $this->rspJson["suite_access_token"];
    }

    // ---------------------- 第三方开放接口 ----------------------------------
    //
    //
    /**
     * @brief GetPreAuthCode : 获取预授权码
     *
     * @link https://work.weixin.qq.com/api/doc#10975/获取预授权码
     *
     * @return string pre_auth_code
     */
    public function GetPreAuthCode()
    {
        self::_HttpCall(self::GET_PRE_AUTH_CODE, 'GET', null);
        return $this->rspJson["pre_auth_code"];
    }

    /**
     * @brief SetSessionInfo : 设置授权配置
     *
     * @link https://work.weixin.qq.com/api/doc#10975/设置授权配置
     *
     * @param $SetSessionInfoReq
     */
    public function SetSessionInfo(SetSessionInfoReq $SetSessionInfoReq)
    {
        $args = $SetSessionInfoReq->FormatArgs();
        self::_HttpCall(self::SET_SESSION_INFO, 'POST', $args);
    }

    /**
     * @brief GetPermanentCode : 获取企业永久授权码
     *
     * @link https://work.weixin.qq.com/api/doc#10975/获取企业永久授权码
     *
     * @param string $temp_auth_code 临时授权码
     *
     * @return GetPermanentCodeRsp
     */
    public function GetPermanentCode($temp_auth_code)
    {
        $args = array("auth_code" => $temp_auth_code);
        self::_HttpCall(self::GET_PERMANENT_CODE, 'POST', $args);
        return GetPermanentCodeRsp::ParseFromArray($this->rspJson);
    }

    /**
     * @brief GetAuthInfo : 获取企业授权信息
     *
     * @link https://work.weixin.qq.com/api/doc#10975/获取企业授权信息
     *
     * @param string $auth_corpid
     * @param string $permanent_code  永久授权码
     *
     * @return GetAuthInfoRsp
     */
    public function GetAuthInfo($auth_corpid, $permanent_code)
    {
        Utils::checkNotEmptyStr($auth_corpid, "auth_corpid");
        Utils::checkNotEmptyStr($permanent_code, "permanent_code");
        $args = array(
            "auth_corpid" => $auth_corpid,
            "permanent_code" => $permanent_code
        );
        self::_HttpCall(self::GET_AUTH_INFO, 'POST', $args);
        return GetAuthInfoRsp::ParseFromArray($this->rspJson);
    }

    /**
     * @brief GetAdminList : 获取应用的管理员列表
     *
     * @link https://work.weixin.qq.com/api/doc#10975/获取应用的管理员列表
     *
     * @param string $auth_corpid
     * @param int $agentid
     *
     * @return GetAdminListRsp
     */
    public function GetAdminList($auth_corpid, $agentid)
    {
        Utils::checkNotEmptyStr($auth_corpid, "auth_corpid");
        Utils::checkIsUInt($agentid, "agentid");
        $args = array(
            "auth_corpid" => $auth_corpid,
            "agentid" => $agentid
        );
        self::_HttpCall(self::GET_ADMIN_LIST, 'POST', $args);
        return GetAdminListRsp::ParseFromArray($this->rspJson);
    }

    /**
     * @brief GetUserinfoBy3rd :第三方根据code获取企业成员信息
     *
     * @link https://work.weixin.qq.com/api/doc#10975/第三方根据code获取企业成员信息
     *
     * @param string $code
     *
     * @return GetUserinfoBy3rdRsp
     */
    public function GetUserinfoBy3rd($code)
    {
        self::_HttpCall(self::GET_USER_INFO_BY_3RD, 'GET', array('code' => $code));
        return GetUserinfoBy3rdRsp::ParseFromArray($this->rspJson);
    }

    /**
     * @brief GetUserDetailBy3rd : 第三方使用user_ticket获取成员详情
     *
     * @link https://work.weixin.qq.com/api/doc#10975/第三方使用user_ticket获取成员详情
     *
     * @param string $user_ticket
     *
     * @return GetUserDetailBy3rdRsp
     */
    public function GetUserDetailBy3rd($user_ticket)
    {
        Utils::checkNotEmptyStr($user_ticket, "user_ticket");
        $args = array("user_ticket" => $user_ticket);
        self::_HttpCall(self::GET_USER_DETAIL_BY_3RD, 'POST', $args);
        return GetUserDetailBy3rdRsp::ParseFromArray($this->rspJson);
    }

}
